from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from time import perf_counter
import time

Bool = True
fhq = 'https://ke.qq.com' #手动放入课程url
py = 'https://ke.qq.com'
ly = 'https://ke.qq.com'
win = 'https://ke.qq.com'
class_list = [fhq , py , ly , win]
in_class = int(input('0语文   1数学   2英语  3体育\n'))

wd = webdriver.Chrome()
wd.implicitly_wait(10)
wd.get(class_list[in_class])

if input('登陆完成?\n'): #无自动登陆模块，手动登陆后回车
    pass
else:
    print('waiting to start...')
    time.sleep(1)

def class_over(): #下课间
    # element = wd.find_element_by_css_selector('[class="im-dialog small"]')
    # WebDriverWait(wd,5).until(EC.invisibility_of_element_located(element))
    wd.find_element_by_css_selector('[class="im-dialog small"]')
    print("\r" + '进入失败或者下课时间，刷新页面', end="")
    time.sleep(60)
    wd.refresh()

def is_class(): #上课判断
    wd.find_element_by_css_selector('[class="ke_overlay_content live"]')
    print("\r" + '老师没上班，等待页面', end="")
    time.sleep(30)
    wd.find_element_by_css_selector('[class="study-body mr"]').click()

def Sign_in(): #签到
    wd.find_element_by_css_selector('[class="s-btn s-btn--primary s-btn--m"]')

    aaa = webdriver.Chrome() #弹个窗提醒一下
    aaa.close()
    time.sleep(3)

    wd.find_element_by_css_selector('[class="s-btn s-btn--primary s-btn--m"]').click()
    print("\r" + '签到成功\n\n', end="")
    time.sleep(2)
    wd.find_element_by_css_selector('[class="s-btn s-btn--primary s-btn--m"]').click()

while Bool:
    print("\r" + 'waiting...', end="")
    time.sleep(3)
    try:
        wd.find_element_by_css_selector('[class="loki-interact loki-playbutton"]').click()
        print("\r" + '上课咯', end="")
    except:
        pass

    try:
        print("\r" + 'waiting..', end="")

        try: #签到元素
            Sign_in()
        except:
            pass

        try: #下课弹窗，60s刷新
            class_over()
            print('下课间')
        except:
            pass

        try: #判断是否到达上课时间，60s刷新
            is_class()
            print("\r" + '上课了', end="")
        except:
            pass

        try:
            wd.find_element_by_css_selector('[class="head-text"]') #选择题元素
            print("\r" + '选择题，可忽略，反正老师不屌', end="")
        except:
            pass

    except Exception as e:
        if 'no such element' in str(e):
            pass
        else:
            print('',e)

'''
<div class="im-dialog react-draggable" style="width: 450px; height: 250px; transform: translate(-225px, -125px);">
<div class="dialog-head drag-handler"><span class="head-text">单选题</span></div><div class="dialog-body">
<div class="dialog-content"><div class="tip"></div><div class="choice-group doing">
<div class="s-f-rc-list list--block"><div class="s-f-rc-item"><i class="icon-css-rc icon-css-radio item-icon">
</i>A</div>
<div class="s-f-rc-item"><i class="icon-css-rc icon-css-radio item-icon">
</i>B</div>
</div></div></div></div>
<div class="btn-group"><span class="s-btn s-btn--primary s-btn--m disabled">确定</span></div>
<div class="count-down"><div class="xq_num">04:40</div><div class="xq_percent"><div class="bar" style="width: 93.3333%;">
</div></div></div></div>

<div class="im-dialog">
<div class="dialog-head"></div>
<div class="dialog-body">
<div class="dialog-content">
<div class="tip">请在倒计时结束前，完成课堂签到</div></div></div>
<div class="btn-group"><span class="s-btn s-btn--primary s-btn--m">签到</span></div>
<div class="count-down"><div class="xq_num">01:49</div>
<div class="xq_percent"><div class="bar" style="width: 90.8333%;"></div></div></div></div>

'''